package com.javapda.c328r;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

import com.javapda.c328r.command.ComediaLightFrequencyType;
import com.javapda.c328r.command.ComediaPreviewColorType;
import com.javapda.c328r.command.ComediaPreviewJpegResolution;
import com.javapda.c328r.command.ComediaPreviewResolution;
import com.javapda.c328r.command.ComediaSnapshotType;
import com.javapda.camera.CameraConfiguration;
import com.javapda.util.FileUtil;

public class C328rCameraConfiguration implements CameraConfiguration {
	private static final String CFG_PROP_PREFIX = "cfg-";
	private Map cfg=new TreeMap();
	private int previewColorType;
	private int previewResolution;
	private int previewJpegResolution;
	private int packageSize;
	private int snapshotType;
	private int lightFrequency;
	private boolean snapshotCompressed;
	
	/**
	 * @return the packageSize
	 */
	public int getPackageSize() {
		return packageSize;
	}
	/**
	 * @param packageSize the packageSize to set
	 */
	public void setPackageSize(int _packageSize) {
		this.packageSize = Math.max(64,Math.min(_packageSize,512));
	}
	public C328rCameraConfiguration() {
		super();
		
		// here we pull configuration from the jar, but will want to get from user's area if available
		Properties props = FileUtil.getPropertiesFromClasspathFile("com/javapda/c328r/c328.configuration.properties");
		// pull all cfg-prefixed properties
		for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			if ( key.startsWith(CFG_PROP_PREFIX) ) {
				String value = props.getProperty(key);
				setCfgProp(key,value);
			}
		}
		// check with System for matching params
		
		setPreviewColorType(ComediaPreviewColorType.convert(props.getProperty("cfg-preview-colortype",""+ComediaPreviewColorType.convert(ComediaPreviewColorType.JPEG))));
		setPreviewResolution(ComediaPreviewResolution.convert(props.getProperty("cfg-preview-resolution",""+ComediaPreviewResolution.convert(ComediaPreviewResolution.RES_160x120))));
		setPreviewJpegResolution(ComediaPreviewJpegResolution.convert(props.getProperty("cfg-preview-jpeg-resolution",""+ComediaPreviewJpegResolution.convert(ComediaPreviewJpegResolution.RES_640x480))));
		setPackageSize(Integer.parseInt(props.getProperty("cfg-packagesize","512")));
		setLightFrequency(ComediaLightFrequencyType.convert(props.getProperty("cfg-light-frequency", ComediaLightFrequencyType.convert(ComediaLightFrequencyType.FREQ_60_HZ))));
		setSnapshotCompressed(props.getProperty("cfg-snapshot-compression","true").toLowerCase().equals("true")); 
		
		// Q? Should we update the user's configuration at this time?
	}
	private void setCfgProp(String cfgprop, String val) {
		System.out.println("Setting CFG PROP: "+cfgprop + "-----"+val);
		cfg.put(cfgprop,val);
	}
	public Set getConfigurationAttributes() {
		return cfg.keySet();
	}

	public Object getConfigurationValue(String configurationAttribute) {
		return cfg.get(configurationAttribute);
	}

	public Map getErrorHelp() {
		return null;
	}

	public Map getAttributeHelp() {
		return FileUtil.getPropertiesFromClasspathFile("com/javapda/c328r/c328.configuration.properties");
	}

	public int getPreviewColorType() {
		return previewColorType;
	}

	public void setPreviewColorType(int colorType) {
		this.previewColorType=colorType;
	}
	/**
	 * @return the previewResolution
	 */
	public int getPreviewResolution() {
		return previewResolution;
	}
	/**
	 * @param previewResolution the previewResolution to set
	 */
	public void setPreviewResolution(int previewResolution) {
		this.previewResolution = previewResolution;
	}
	/**
	 * @return the previewJpegResolution
	 */
	public int getPreviewJpegResolution() {
		return previewJpegResolution;
	}
	/**
	 * @param previewJpegResolution the previewJpegResolution to set
	 */
	public void setPreviewJpegResolution(int previewJpegResolution) {
		this.previewJpegResolution = previewJpegResolution;
	}
	/**
	 * @return the snapshotType
	 */
	public int getSnapshotType() {
		return snapshotType;
	}
	/**
	 * @param snapshotType the snapshotType to set
	 */
	public void setSnapshotType(int snapshotType) {
		this.snapshotType = snapshotType;
	}
	/**
	 * @return the lightFrequency
	 */
	public int getLightFrequency() {
		return lightFrequency;
	}
	/**
	 * @param lightFrequency the lightFrequency to set
	 */
	public void setLightFrequency(int lightFrequency) {
		this.lightFrequency = lightFrequency;
	}
	/**
	 * @return the snapshotCompressed
	 */
	public boolean isSnapshotCompressed() {
		return snapshotCompressed;
	}
	/**
	 * @param snapshotCompressed the snapshotCompressed to set
	 */
	public void setSnapshotCompressed(boolean snapshotCompressed) {
		this.snapshotCompressed = snapshotCompressed;
		if ( isSnapshotCompressed() ) {
			setSnapshotType(ComediaSnapshotType.COMPRESSED);
		} else {
			setSnapshotType(ComediaSnapshotType.UNCOMPRESSED);
		}
	}

}
